import {ApiData, MyCat, MyTopic, PageData} from "@/interfaces/apiResponse.ts";
import myAxios from "@/plugins/axios";
import router from "@/router";
import {ElMessageBox} from "element-plus";

export default () => {
    const cats = ref<MyCat[]>();
    const cat = ref<MyCat>();
    const topics = ref<PageData<MyTopic>>();
    const topicDetail = ref<MyTopic>();
    const form = reactive({
        title: '',
        content: '',
        cat_id: ''
    })
    const getCats = async () => {
        const res = await myAxios.request<ApiData<MyCat[]>>({
            url: 'cat'
        })
        cats.value = res.data;
    }
    const getTopics = async (page = 1, params = {}) => {
        //key=abc&keywords=bbb
        const paramsStr = Object.entries(params).map(e => e.join('=')).join('&');
        topics.value = await myAxios.request<PageData<MyTopic>>({
            url: `topic?page=${page}&` + paramsStr
        });
    }
    const getOneCatTopics = async (page = 1, params = {}) => {
        const c_id = myLocalStore().get('c_id');
        //key=abc&keywords=bbb
        const paramsStr = Object.entries(params).map(e => e.join('=')).join('&');
        topics.value = await myAxios.request<PageData<MyTopic>>({
            url: `per_cat/${c_id}?page=${page}&` + paramsStr
        });
    }
    const getTopicDetail = async (t_id: any) => {
        const res = await myAxios.request<ApiData<MyTopic>>({
            url: `topic/${t_id}`
        });
        topicDetail.value = res.data;
    }
    const addTopic = async (data: any) => {
        return await myAxios.request<ApiData<MyTopic>>({
            url: `topic`,
            method: 'POST',
            data
        });
    }
    const editTopic = async (data: any) => {
        return await myAxios.request<ApiData<MyTopic>>({
            url: `topic/${data.id}`,
            method: 'PUT',
            data
        });
    }
    const delTopic = async (t_id: any) => {
        try {
            await ElMessageBox.confirm(
                '您确认要删除该帖子吗?',
                '删除提示',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
            await myAxios.request<ApiData<null>>({
                url: `topic/${t_id}`,
                method: 'DELETE',
            });
            //跳转
            const route = router.resolve({name: 'home'});
            location.href = route.fullPath;
        } catch (error) {

        }
    }
    const adminDelTopic = async (t_id: any) => {
        try {
            await ElMessageBox.confirm(
                '您确认要删除该帖子吗?',
                '删除提示',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
            await myAxios.request<ApiData<null>>({
                url: `topic/${t_id}`,
                method: 'DELETE',
            });
            //跳转
            const route = router.resolve({name: 'topic.index'});
            location.href = route.fullPath;
        } catch (error) {

        }
    }
    const getCat = async (c_id: any) => {
        const res = await myAxios.request<ApiData<MyCat>>({
            url: `cat/${c_id}`
        });
        cat.value = res.data;
    }
    const addCat = async (data: any) => {
        return await myAxios.request<ApiData<MyCat>>({
            url: `cat`,
            method: 'POST',
            data
        });
    }
    const editCat = async (data: any) => {
        return await myAxios.request<ApiData<MyTopic>>({
            url: `cat/${data.value.id}`,
            method: 'PUT',
            data:data.value
        });
    }
    const delCat = async (c_id: any) => {
        try {
            await ElMessageBox.confirm(
                '您确认要删除该大类吗?',
                '删除提示',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
            await myAxios.request<ApiData<null>>({
                url: `cat/${c_id}`,
                method: 'DELETE',
            });
            await getCats();
        } catch (error) {

        }

    }
    const changeSort = async ()=>{
        //console.log(cats.value?.map(c=>c.id));
        await myAxios.request<ApiData<null>>({
            url: `cat/change_sort`,
            method: 'POST',
            data:{'cat':cats.value?.map(c=>c.id)}
        });
    }
    return {
        cats, getCats, topics, getTopics, getOneCatTopics,
        getTopicDetail, topicDetail, form, addTopic, editTopic,
        delTopic,cat,addCat,getCat,editCat,delCat,changeSort,adminDelTopic
    };
}